package com.lanou.zakerproject.hotspot;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lanou.zakerproject.R;
import com.lanou.zakerproject.base.VolleySingleton;
import com.lanou.zakerproject.db.DbTool;
import com.lanou.zakerproject.tool.GsonRequest;


/**
 * Created by dllo on 16/1/15.
 */
public class HotSpotAddHobbyFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemClickListener {
    private TextView textView;
    private EditText editText;
    private GridView gridView;
    private ImageView imageView;
    private HobbyGridViewAdapter hobbyGridViewAdapter;
    private DbTool dbTool;
    private TagData tagData;
    private String url = "http://iphone.myzaker.com/zaker/user_tag.php?_appid=AndroidPhone&_bsize=1080_1920&_dev=97&_lat=4.9E-324&_lng=4.9E-324&_mac=08%3A00%3A27%3A3f%3A7a%3A01&_mcode=B51557E3&_net=wifi&_nudid=7891a15eb7d34771&_os=5.1_GoogleNexus5-5.1.0-API22-1080x1920&_os_name=GoogleNexus5-5.1.0-API22-1080x1920&_udid=5.1_GoogleNexus5-5.1.0-API22-1080x1920.08%3A00%3A27%3A3f%3A7a%3A01&_v=6.4.5&_version=6.44&act=chk";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_hotspotaddhobby, null);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        textView = (TextView) view.findViewById(R.id.tab_look_hobby_tv);
        editText = (EditText) view.findViewById(R.id.edit_add_hobby);
        gridView = (GridView) view.findViewById(R.id.gridView_addhobby);
        imageView = (ImageView) view.findViewById(R.id.add_hobby);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        dbTool = new DbTool(getActivity());
        getRequsetData(url);
        gridView.setOnItemClickListener(this);
        textView.setOnClickListener(this);
        imageView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add_hobby:
                String title = editText.getText().toString();
                if (title.length() > 0) {
                    dbTool.addData(title);
                }
                break;
            case R.id.tab_look_hobby_tv:
                hobbyGridViewAdapter.update(dbTool.allData());
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String title_get = tagData.getData().getHot_tags().get(position);
        dbTool.addData(title_get);
    }

    /**
     * 解析数据的方法
     *
     * @param urls 接口
     */
    private void getRequsetData(String urls) {
        GsonRequest<TagData> gsonRequest = new GsonRequest<>(urls, TagData.class, new Response.Listener<TagData>() {
            @Override
            public void onResponse(TagData response) {
                hobbyGridViewAdapter = new HobbyGridViewAdapter(getActivity(), response);
                gridView.setAdapter(hobbyGridViewAdapter);
                tagData = response;
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });
        VolleySingleton.getInstance()._addRequest(gsonRequest);
    }


}
